package dk.pun.dums.client.view;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

import dk.pun.dums.client.widget.CharacterTabWidget;

public class MainViewImpl extends Composite implements MainView {

	private static MainViewUiBinder uiBinder = GWT.create(MainViewUiBinder.class);

	@UiTemplate("MainView.ui.xml")
	interface MainViewUiBinder extends UiBinder<Widget, MainViewImpl> {}

	@UiField protected transient Label messageLabel;
	@UiField protected transient TopBarViewImpl topBarView;
	@UiField protected transient SelectCharacterViewImpl selectCharacterView;
	@UiField protected transient TabLayoutPanel tabLayoutPanel;

	private final transient Map<String, Widget> characters;
	
	public MainViewImpl() {
		super();
		this.initWidget(uiBinder.createAndBindUi(this));
		this.tabLayoutPanel.setAnimationDuration(1000);
		this.characters = new HashMap<String, Widget>();
	}

	@Override
	public void setMessage(final String textMessage) {
		if (textMessage == null || textMessage.isEmpty()) {
			this.messageLabel.setText("");
			this.messageLabel.setVisible(false);
		} else {
			this.messageLabel.setText(textMessage);
			this.messageLabel.setVisible(true);
		}
	}
	
	@Override
	public void addCharacter(final ViewCharacterView viewCharacterView, final String name) {		
		if (!this.characters.containsKey(name)) {
			final Widget widget = viewCharacterView.asWidget();
			this.characters.put(name, widget);
			this.tabLayoutPanel.add(widget, new CharacterTabWidget(name, this));
		}
		this.setSelectedCharacter(name);
	}
	
	@Override
	public void removeCharacter(final String name) {
		final Widget widget = this.characters.get(name);
		if (widget != null) {
			this.characters.remove(name);
			this.tabLayoutPanel.remove(widget);
			GWT.log("Tab removed: " + widget.toString());
		}
	}
	
	@Override
	public void renameCharacter(final String oldName, final String newName) {
		final Widget widget = this.characters.get(oldName);
		if (widget != null) {
			final CharacterTabWidget tabWidget = (CharacterTabWidget) this.tabLayoutPanel.getTabWidget(widget);
			tabWidget.setTabTitle(newName);
			this.characters.remove(oldName);
			this.characters.put(newName, widget);
		}
	}
	
	@Override
	public void setSelectedCharacter(final String name) {
		final Widget widget = this.characters.get(name);
		if (widget != null) {
			this.tabLayoutPanel.selectTab(widget);
		}
	}
	
	@Override
	public TopBarView getTopBarView() {
		return this.topBarView;
	}
	
	@Override
	public SelectCharacterView getSelectCharacterView() {
		return this.selectCharacterView;
	}
	
	@Override
	public void setPresenter(final Presenter presenter) {
		// Presenter is not used
	}

	@Override
	public Widget asWidget() {
		return this;
	}

}
